package com.hhq.sso.auth.base.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hhq.sso.common.api.CommonResult;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <p>
 * Description: 自定义用户未登录的处理（未携带token）
 * </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/6/20 11:36
 */
@Component
public class AuthEntryPointHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        String value = new ObjectMapper().writeValueAsString(CommonResult.fail("未携带token！"));
        response.getWriter().write(value);
    }
}
